﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repository;

namespace GameSchoolApplication.Controllers
{
    public class VideoController : Controller
    {
        private GameSchoolWebContext _db = new GameSchoolWebContext();
        private GameRepository _game = new GameRepository();
        private LevelRepository _level = new LevelRepository();
       
		/// <summary>
		/// Create a new video
		/// </summary>
		/// <param name="levelId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Create(int levelId)
        {
            Level level = _game.GetLevelById(levelId); 
            return View(new Video { Level = level, LevelId = levelId });
        }

		/// <summary>
		/// Sends the video to database
		/// </summary>
		/// <param name="video"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult Create(Video video)
        {
            if (ModelState.IsValid)
            {
                _level.AddVideo(video);
                return RedirectToAction("Details", "Level", new { id = video.LevelId });
            }
            return View(video);
        }

		/// <summary>
		/// Gets a specified video by id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet]
        public ActionResult Watch(int id)
        {
            Video video = _level.GetVideoById(id);
            return View(video);
        }

      
        [HttpPost]
        public ActionResult AddComment(Comment comment)
        {
            Person currentUser = _db.Persons.Where(m => m.UserName == User.Identity.Name).SingleOrDefault();
            if (!String.IsNullOrEmpty(comment.Content))
            {
                var commentOfVideo = new Comment();
                commentOfVideo.AuthorId = currentUser.Id;
                commentOfVideo.VideoId = comment.VideoId;
                commentOfVideo.Content = comment.Content;
                commentOfVideo.CommentDate = DateTime.Now;
                                       
                _level.AddCommentToVideo(commentOfVideo);
            }
            var comments = _level.GetComments(comment.VideoId);

            return Json(comments, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetAllComments(int videoId)
        {
            var comments = _level.GetComments(videoId);

            return Json(comments, JsonRequestBehavior.AllowGet);
        }


       /* public void Like(int? id)
        {

            Person currentUser = _db.Persons.Where(m => m.UserName == User.Identity.Name).SingleOrDefault();
            
            if (id.HasValue)
            {
                _level.LikeComment(new Like { CommentId = id.Value, UserId = currentUser.Id });
                var likeUsers = _level.GetAllLikes(id.Value);
                var returnValue = from like in likeUsers
                                  select new { User = like.UserName };

                return Json(returnValue);
            }
            else
                return Json("");
        }*/

        public void SuperLike()
        { 
        
        }

        public void Edit()
        { 
        
        }

    }
}
